package com.itheima.tilas.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.tilas.Utils.ResultUtil;
import com.itheima.tilas.dto.SelectEmpCountGender;
import com.itheima.tilas.dto.SelectEmpCountJob;
import com.itheima.tilas.dto.SelectEmpDetailDTO;
import com.itheima.tilas.dto.SelectEmpPageDTO;
import com.itheima.tilas.entity.Emp;
import com.itheima.tilas.entity.Expr;
import com.itheima.tilas.mapper.EmpMapper;
import com.itheima.tilas.mapper.ExprMapper;
import com.itheima.tilas.po.InsertEmpOneParam;
import com.itheima.tilas.po.SelectEmpPageParam;
import com.itheima.tilas.po.UpdateEmpOneParam;
import com.itheima.tilas.service.EmpService;
import com.itheima.tilas.vo.CountJobVo;
import com.itheima.tilas.vo.PageVo;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author 132
 */
@Log4j2
@Service
public class EmpServiceImpl implements EmpService {
    @Resource
    EmpMapper empMapper;
    @Resource
    ExprMapper exprMapper;

    @Override
    public ResultUtil selectEmpPageAll(SelectEmpPageParam pageParam) {
        //注意: pageHelper 会自动执行count sql,并且会自动拼接Limit
        Page<Object> objects = PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<SelectEmpPageDTO> pageDTO = empMapper.selectEmpPageAll(pageParam);
        PageVo<SelectEmpPageDTO> pageVo = new PageVo<>();
        pageVo.setTotal(objects.getTotal());
        pageVo.setRows(pageDTO);
        return ResultUtil.success(pageVo);
    }

    @Override
    public SelectEmpDetailDTO selectEmpById(Integer id) {
        SelectEmpDetailDTO dto = empMapper.selectEmpById(id);
        List<Expr> list = exprMapper.selectExprByEmpId(dto.getId());
        dto.setExprList(list);
        return dto;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertEmpOne(InsertEmpOneParam one) {
        int rowsEmp = empMapper.insertEmpOne(one);
        log.info("员工回显的主键{}", one.getId());
        if (CollectionUtils.isNotEmpty(one.getExprList())) {
            //赋值员工id以及时间
            List<Expr> exprList = one.getExprList();
            exprList = exprList.stream().map(o -> {
                o.setEmpId(one.getId());
                o.setCreateTime(LocalDateTime.now());
                o.setUpdateTime(LocalDateTime.now());
                return o;
            }).toList();
            int flag = exprMapper.batchInsertExpr(exprList);
            log.info("插入员工履历{}", flag);
        }
        return rowsEmp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteEmpByIds(List<Integer> ids) {
        //先删除员工
        int flag = empMapper.deleteEmpByIds(ids);
        //在删除员工履历
        int flag1 = exprMapper.bachDeleteExprByEmpId(ids);

        return flag;
    }

        @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateEmpOne(UpdateEmpOneParam emp) {
        //1.更新员工信息
        int flag = empMapper.updateEmpOne(emp);
        //2.再删除该员工的所有履历
        int exprDelRows = exprMapper.bachDeleteExprByEmpId(List.of(emp.getId()));
        log.info("删除{}条旧履历数据", exprDelRows);
        //3.新增一个履历
        if (CollectionUtils.isNotEmpty(emp.getExprList())) {
            List<Expr> exprList = emp.getExprList();
            exprList = exprList.stream().map(o -> {
                o.setEmpId(emp.getId());
                o.setCreateTime(LocalDateTime.now());
                o.setUpdateTime(LocalDateTime.now());
                return o;
            }).toList();
            int rowsInsert = exprMapper.batchInsertExpr(exprList);
            log.info("插入员工履历{}", rowsInsert);
        }
        return flag;
    }

    @Override
    public List<Emp> selectEmpAll() {
        return empMapper.selectEmpAll();
    }

    @Override
    public List<SelectEmpCountGender> selectEmpCountGender() {
        return empMapper.selectEmpCountGender();
    }

    @Override
    public ResultUtil selectEmpCountJob() {
        List<SelectEmpCountJob> jobs = empMapper.selectEmpCountJob();
        if (CollectionUtils.isEmpty(jobs)) {
            return ResultUtil.fail("本次查询失败");
        }
        CountJobVo countJobVo = new CountJobVo();
        jobs.forEach(o -> {
            countJobVo.getJobList().add(o.getJobList());
            countJobVo.getDataList().add(o.getDataList());
        });
        return ResultUtil.success("本次查询成功", countJobVo);
    }
}
